var v=require(VUECOMPONENT).default;
window._isapp=false;
window.query=getquery();

window.vm=new Vue({
    el:'#main',	
    template:"<div><page ref='pages'></page></div>",
	data:{
		//defaultImg:"http://imgqn.dabeiduo.com/default_member_avatar.png"
	},
    components:{
        "page":v
    }, 
	methods:{
		//格式化日期
		formatDate:function(s){
			if(!s)return "";
			var date=new Date(parseInt(s));
			var dateStr="";
			dateStr+=date.getFullYear()+"-";
			dateStr+=(date.getMonth()+1>9?date.getMonth()+1:"0"+(date.getMonth()+1))+"-";
			dateStr+=(date.getDate()>9?date.getDate():"0"+date.getDate())+" ";
			dateStr+=(date.getHours()>9?date.getHours():"0"+date.getHours())+":";
			dateStr+=(date.getMinutes()>9?date.getMinutes():"0"+date.getMinutes())+":";
			dateStr+=date.getSeconds()>9?date.getSeconds():"0"+date.getSeconds();	
			return dateStr;
		},	
		up(file,filetype,callback){
			if(!FormData){
				UI.alert('浏览器版本过低，请升级或者更换浏览器');
			}
			var formdata=new FormData();
			filetype=filetype?filetype:'img'
			formdata.append('type',filetype);

			for(var i=0;i<file.files.length;i++){
				if(file.files[i].size>2*1024*1024){
					continue;
				}
				if(filetype=='img'){
					formdata.append('file'+i,file.files[i]);
					/* this.zipImg(file.files[i],function(blob){
						console.log(blob)
						formdata.append('file'+i,blob);
					},200,'',0.85) */
				}else{
					formdata.append('file'+i,file.files[i]);
				}
			}
			if(file.getAttribute('old')){
				formdata.append('old',file.getAttribute('old'))
			}
			var xhr =new XMLHttpRequest();	
			xhr.open('post',window.host+'upfile', true);
			xhr.onload=function(res){
				if(callback){
					console.log(xhr.responseText)
					let data=JSON.parse(xhr.responseText);
					if(data.state==0){
						UI.alert(data.msg)
						file.value=""
					}else{
						callback(data.data);
					}
				}
			}
			xhr.send(formdata)
		},
					/**
			 * 上传图片处理
			 *  
			 * @param {Object} file 文件域file.files[0]对象
			 * @param {Object} callback 压缩成功的回调函数 callback(Blob对象，base64图片编码)
			 * @param {Object} w 指定处理后的图片宽度  若只指定宽度，高度则按宽度等比缩放，都不指定按原图尺寸
			 * @param {Object} h 指定处理后的图片高度若只指定高度，宽度则按宽度等比缩放，都不指定按原图尺寸
			 * @param {Object} compressIndex 压缩品质 0-1 默认0.9
			 */
		 zipImg:function(file,callback,w,h,compressIndex){
			var canvas = document.createElement('canvas');
			if(!FileReader || !canvas.toBlob){
				alert('上传文件无法处理，请升级浏览器')
				return false;
			}
			 if(!file.name.match(/(.jpg|.jpeg|.png)$/i)){
				alert('图片格式错误');
				return false;
			}
			
			var reader = new FileReader();
			var img = new Image();
			compressIndex=compressIndex?compressIndex:0.9;
			reader.readAsDataURL(file);
			reader.onload = function(e) {
			  img.src = e.target.result;
			  img.onload=function(){
				  
				  var context = canvas.getContext('2d');		  
				  var targetW,targetH;
				  if(w && h){
					  targetW=w;
					  targetH=h;
				  }else if(w && !h){
					  targetH=parseInt(w*this.height/this.width);
					  targetW=w;
				  } else if(!w && h){
					  targetW=parseInt(h*this.width/this.height);
					  targetH=h;
				  }else{
					  targetW=this.width;
					  targetH=this.height;
				  }
				  console.log(targetW,targetH)
				  
				  canvas.width = targetW;
				  canvas.height = targetH;
				  //核心JS
				  context.drawImage(img,0,0,targetW,targetH);
				  canvas.toBlob(function(a){
					  callback(a,canvas.toDataURL('image/jpeg',compressIndex));
				  },'image/jpeg',compressIndex);
			  }	  
			}		
		}
	},
	mounted(){
		if(location.href.indexOf('login.html')<0 && location.href.indexOf('index.html')<0){
			ajax({
				url:'login/islogin',
				success:function(res){
					if(res.state!=1){
						window.top.location.href='login.html';
					}
				} 
			});
		}
	}
});

